﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ThoiTrang.Business;
using ThoiTrang.Entities;

public partial class Admin_article_add : System.Web.UI.Page
{ 
    #region init load
    void loadMainCat()
    {
        ddlCat.DataTextField = a_CatMainColumns.UName.ToString();
        ddlCat.DataValueField = a_CatMainColumns.CatID.ToString();
        ddlCat.DataSource= ThoiTrang.Business.a_CatMainService.Geta_CatMainList(a_CatMainColumns.UName, ThoiTrang.Data.OrderDirection.ASC);
        ddlCat.DataBind();
        ddlCat.Items.Insert(0,new ListItem("Select one",""));
    }

    void loadSubCat()
    {
        if (ddlCat.Items.Count > 0)
        {
            ddlSubCat.DataTextField = a_CatSubColumns.UName.ToString();
            ddlSubCat.DataValueField = a_CatSubColumns.SubCatID.ToString(); 
            if (ddlCat.SelectedValue == "")
            {
                ddlSubCat.DataSource = null;
            }
            else
            {  
                ddlSubCat.DataSource = ThoiTrang.Business.a_CatSubService.Geta_CatSubListByCatMain(
                    Convert.ToInt32(ddlCat.SelectedValue));
            }
            ddlSubCat.DataBind();
        }
    }

    void loadCountry()
    {
        ddlCountry.DataTextField = a_CountryColumns.CountryNameVN.ToString();
        ddlCountry.DataValueField = a_CountryColumns.CountryID.ToString(); ;
        ddlCountry.DataSource = ThoiTrang.Business.a_CountryService.Geta_CountryList(a_CountryColumns.CountryNameVN, ThoiTrang.Data.OrderDirection.ASC);
        ddlCountry.DataBind();
    }

    void loadYear()
    {
        DateTime dt = DateTime.Now;
        ddlYear.Items.Add(new ListItem((dt.Year-1).ToString(),(dt.Year-1).ToString()));
        ddlYear.Items.Add(new ListItem(dt.Year.ToString(), dt.Year.ToString()));
        ddlYear.Items.Add(new ListItem((dt.Year + 1).ToString(), (dt.Year + 1).ToString()));
        ddlYear.Items.Add(new ListItem((dt.Year +2).ToString(), (dt.Year +2).ToString()));
        ddlYear.Items.Add(new ListItem((dt.Year + 3).ToString(), (dt.Year + 3).ToString()));
        ddlYear.SelectedIndex = 1;

    }


    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fckBodyEN.BasePath = CommonFunc.FckEditorBasePath();
            fckBodyEN.SkinPath = CommonFunc.FckEditorSkinPath();
            fckBodyVN.BasePath = CommonFunc.FckEditorBasePath();
            fckBodyVN.SkinPath = CommonFunc.FckEditorSkinPath();

            loadMainCat();
            loadCountry();
            loadYear();
            loadToEdit();
        }       
    }
   
    protected void ddlCat_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadSubCat();
    }

    void loadToEdit()
    {
        string aid = Request.QueryString["aid"] == null ? "" : Request.QueryString["aid"];
        if (aid != "")
        {
            a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));
            btnUpdate.Visible = true;
            btnUploadImageDetail.Visible = true;

            a_User uid = a_UserService.Geta_User(aa.PostBy_UserID);

            txtAuthor.Text =uid.Firstname.Trim()+" "+ uid.Lastname.Trim();
            //txtBodyEN.Text = aa.KBody;
            //txtBodyVN.Text = aa.UBody;
            fckBodyEN.Value = aa.KBody;
            fckBodyVN.Value = aa.UBody;

            txtBrand.Text = aa.Brand;
            txtKeywords.Text = aa.UniqueKeywords;
            txtPubDate.Text = aa.PublishDate.ToString("dd MMM yyyy");
            txtTitleEN.Text = aa.KTitle;
            txtTitleVN.Text = aa.UTitle;

            ddlCat.SelectedValue = aa.MainCatID.ToString();
            loadSubCat();

            ddlCountry.SelectedValue = aa.CountryID.ToString();
            ddlFeature.SelectedValue = aa.FeaturedType.ToString()  ;
            ddlLayoutFormat.SelectedValue = aa.LayoutFormat.ToString();
            ddlPermission.SelectedValue = aa.Permission.ToString();
            ddlPubStatus.SelectedValue = aa.PublishStatus == true ? "1" : "0";
            ddlSeason.SelectedValue = aa.SeasonID.ToString();
            ddlSubCat.SelectedValue = aa.SubCatID.ToString();
            ddlYear.SelectedValue = aa.FYear.ToString();
           
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            a_User myUser = null;
            //if (SessionManager.IsLogged)
            //{
                a_Session ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
                myUser = a_UserService.Geta_User(ss.CookieID);
            //}
            //else { return; }

            ThoiTrang.Entities.a_Article myArtInfo = new a_Article();
            myArtInfo.Brand = txtBrand.Text;
            myArtInfo.CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
            myArtInfo.FeaturedType = Convert.ToByte(ddlFeature.SelectedValue);
            myArtInfo.FYear = Convert.ToInt32(ddlYear.SelectedValue);


            myArtInfo.ID = 0;
            myArtInfo.IsEdited = false;
            myArtInfo.IsFeatured = ddlFeature.SelectedValue != "0";
           // myArtInfo.KBody = txtBodyEN.Text;
            myArtInfo.KBody = fckBodyEN.Value;
            myArtInfo.KTitle = txtTitleEN.Text;
            myArtInfo.LayoutFormat = Convert.ToByte(ddlLayoutFormat.SelectedValue);
            myArtInfo.MainCatID = Convert.ToInt32(ddlCat.SelectedValue);

            a_CatMain mcat = a_CatMainService.Geta_CatMain(myArtInfo.MainCatID);
            myArtInfo.GroupID = mcat.GroupID;

            myArtInfo.Permission = ddlPermission.SelectedValue != "0";



            myArtInfo.PostBy_UserID = myUser.UserID;//userid get form ckid
            myArtInfo.PostDate = DateTime.Now;
            myArtInfo.PublishDate = Convert.ToDateTime(txtPubDate.Text);
            myArtInfo.PublishStatus = ddlPubStatus.SelectedValue != "0";
            myArtInfo.SeasonID = Convert.ToInt32(ddlSeason.SelectedValue);
            myArtInfo.StatPopularity = Convert.ToInt32(ddlPermission.SelectedValue);
            myArtInfo.StatRating = 0;
            myArtInfo.SubCatID = Convert.ToInt32(ddlSubCat.SelectedValue);
            myArtInfo.UAuthor = txtAuthor.Text;
           // myArtInfo.UBody = txtBodyVN.Text;
            myArtInfo.UBody = txtBodyVN.Text;
            myArtInfo.UniqueKeywords = txtKeywords.Text;
            myArtInfo.UTitle = txtTitleVN.Text;

            ThoiTrang.Business.a_ArticleService.Createa_Article(myArtInfo);
            lblErrorMessage.Text = "Add success";
        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Add fail<br>" + ex.Message; ;
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            a_User myUser = null;
            //if (SessionManager.IsLogged)
            //{
                a_Session ss = a_SessionService.Geta_Session(SessionManager.GetCookie());
                myUser = a_UserService.Geta_User(ss.CookieID);
            //}
            //else { return; }

            string aid = Request.QueryString["aid"] == null ? "" : Request.QueryString["aid"];
            if (aid != "")
            {
                a_Article aa = a_ArticleService.Geta_Article(Convert.ToInt32(aid));
            }
            else return;

            ThoiTrang.Entities.a_Article myArtInfo = a_ArticleService.Geta_Article(Convert.ToInt32(aid));

            myArtInfo.Brand = txtBrand.Text;
            myArtInfo.CountryID = Convert.ToInt32(ddlCountry.SelectedValue);
            myArtInfo.FeaturedType = Convert.ToByte(ddlFeature.SelectedValue);
            myArtInfo.FYear = Convert.ToInt32(ddlYear.SelectedValue);

            myArtInfo.IsEdited = true;
            myArtInfo.IsFeatured = ddlFeature.SelectedValue != "0";
            //myArtInfo.KBody = txtBodyEN.Text;
            myArtInfo.KBody = fckBodyEN.Value;
            myArtInfo.KTitle = txtTitleEN.Text;
            myArtInfo.LayoutFormat = Convert.ToByte(ddlLayoutFormat.SelectedValue);
            myArtInfo.MainCatID = Convert.ToInt32(ddlCat.SelectedValue);

            a_CatMain mcat = a_CatMainService.Geta_CatMain(myArtInfo.MainCatID);
            myArtInfo.GroupID = mcat.GroupID;

            myArtInfo.Permission = ddlPermission.SelectedValue != "0";

            myArtInfo.PostBy_UserID = myUser.UserID;//userid get form ckid
            myArtInfo.PostDate = DateTime.Now;
            myArtInfo.PublishDate = Convert.ToDateTime(txtPubDate.Text);
            myArtInfo.PublishStatus = ddlPubStatus.SelectedValue != "0";
            myArtInfo.SeasonID = Convert.ToInt32(ddlSeason.SelectedValue);
            myArtInfo.StatPopularity = Convert.ToInt32(ddlPermission.SelectedValue);

            myArtInfo.SubCatID = Convert.ToInt32(ddlSubCat.SelectedValue);
            myArtInfo.UAuthor = txtAuthor.Text;
            //myArtInfo.UBody = txtBodyVN.Text;
            myArtInfo.UBody = fckBodyVN.Value;
            myArtInfo.UniqueKeywords = txtKeywords.Text;
            myArtInfo.UTitle = txtTitleVN.Text;

            ThoiTrang.Business.a_ArticleService.Updatea_Article(myArtInfo);

            lblErrorMessage.Text = "Update success"; 

        }
        catch (Exception ex)
        {
            lblErrorMessage.Text = "Update fail<br>" + ex.Message; 
        }
    }

    protected void btnUploadImageDetail_Click(object sender, EventArgs e)
    {
        string ckid = Request.QueryString["ckid"];
        string aid = Request.QueryString["aid"];
        Response.Redirect("~/Admin/adm_articleDet_upload.aspx?ckid=" + ckid + "&aid=" + aid);
    }
}
